package com.custom.base.util

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.widget.Toast
import com.custom.base.manager.SDActivityManager


/**
 * 作者：
 * 日期：2021/1/18
 * 说明：
 */
class OtherUtil {
    /**
     * 复制，用于粘贴
     */
    fun copyText(content: CharSequence?) {
        val clip = getNewClipboardManager()
        clip?.setPrimaryClip(ClipData.newPlainText(null, content))
        Toast.makeText(SDActivityManager.instance.lastActivity,"copy success!",Toast.LENGTH_SHORT).show()
    }
    /**
     * 粘贴
     */
    fun pasteText(): CharSequence? {
        var content: CharSequence? = null
        val clip = getNewClipboardManager()
        if (clip!!.hasPrimaryClip()) {
            content = clip.primaryClip?.getItemAt(0)?.text
        }
        return content
    }

    fun getNewClipboardManager(): ClipboardManager? {
        return SDActivityManager.instance.lastActivity?.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
    }
}
